package cn.fnst.facecomp.dao;

import java.io.Reader;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import cn.fnst.facecomp.model.ComparePhotoInfo;

public class CommonDao {
	protected Logger log = Logger.getLogger(getClass());
	
	protected static SqlMapClient sqlMapClient;
	
	private static CommonDao instance;
	
	public static synchronized CommonDao getInstance() throws Exception{
		if(instance == null){
			instance = new CommonDao();
		}
		return instance;
	}
	
	public CommonDao() throws Exception{
		Reader reader = Resources.getResourceAsReader ("SqlMapConfig.xml");
		sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
	}
	
	public void addComparePhotoInfo(ComparePhotoInfo c) throws Exception{
		try {
			long id = Long.valueOf(sqlMapClient.queryForObject("face.getComparePhotoInfoID").toString());
			c.setId(id);
			sqlMapClient.insert("face.insert_comparePhotoInfo", c);
		} catch (SQLException e) {
			e.printStackTrace();
			c.setId(0);
		}
	}
	
	public void updateAddComment(ComparePhotoInfo c) throws Exception{
		sqlMapClient.update("face.update_addComment", c);
	}
	
	public void updateShareInfo(ComparePhotoInfo c) throws Exception{
		sqlMapClient.update("face.update_addShareInfo", c);
	}
	
	public void updateComparePhotoInfo(ComparePhotoInfo c){
		try{
			sqlMapClient.update("face.update_ComparePhotoInfo", c);
		}catch(Exception e){
			log.error("updateComparePhotoInfo error", e);
		}
	}
	
	public long getUsedTimes(String userCard, String date) throws Exception{
		Map m = new HashMap();
		m.put("userCard", userCard);
		m.put("date", date);
		return Long.valueOf(sqlMapClient.queryForObject("face.getUsedTimes", m).toString());
	}
	
}
